package com.shuqi.activity.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.shuqi.common.utils.SqImageManager;
import com.shuqi.controller.R;
import com.shuqi.database.model.BookInfo;

/**
 * 专题详情适配器
 * 
 * @author zhuchen
 * 
 */
public class ProjectDetailsAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private List<BookInfo> list;

    public ProjectDetailsAdapter(Context context) {
        this.inflater = LayoutInflater.from(context);
    }

    public void setDataList(List<BookInfo> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        if (list == null) {
            return 0;
        } else {
            return list.size();
        }
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView != null) {
            view = convertView;
        } else {
            view = inflater.inflate(R.layout.item_projectdetails, parent, false);
        }
        Holder holder = (Holder) view.getTag();
        if (holder == null) {
            holder = new Holder();
            holder.projectdetails_title_image =
                    (ImageView) view.findViewById(R.id.projectdetails_title_image);
            holder.projectdetails_bookname_text =
                    (TextView) view.findViewById(R.id.projectdetails_bookname_text);
            holder.projectdetails_type_text =
                    (TextView) view.findViewById(R.id.projectdetails_type_text);
            holder.projectdetails_author_text =
                    (TextView) view.findViewById(R.id.projectdetails_author_text);
            holder.projectdetails_description_text =
                    (TextView) view.findViewById(R.id.projectdetails_description_text);
            view.setTag(holder);
        }
        // BookInfo info = projectDetailsInfo.getList().get(position);
        BookInfo info = list.get(position);
        if (info != null) {
            holder.projectdetails_bookname_text.setText(info.getBookName());
            holder.projectdetails_type_text.setText(info.getBookType());
            holder.projectdetails_author_text.setText(info.getBookAuthorName());
            holder.projectdetails_description_text.setText(info.getBookIntro());
            // 异步加载图片
            SqImageManager.getImageManager().displayImage(info.getBookCoverImgUrl(),
                    holder.projectdetails_title_image, SqImageManager.IMG_COVER);
        }
        return view;
    }

    private static class Holder {
        public ImageView projectdetails_title_image;// 封面
        public TextView projectdetails_bookname_text;// 书名
        public TextView projectdetails_type_text; // 类型
        public TextView projectdetails_author_text;// 作者
        public TextView projectdetails_description_text;// 简介
    }

}
